package com.boomsecret.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wangjinliang on 2018/8/8.
 */
public class ReentrantLockDemo {

//    private static ReentrantLock lock = new ReentrantLock(true); // 公平锁
    private static ReentrantLock lock = new ReentrantLock(); // 非公平锁

    public static void main(String[] args) throws Exception {
        for(int index = 0; index < 10; index ++) {
            new TaskThread(index).start();
            Thread.sleep(500);
        }
    }

    static class TaskThread extends Thread {
        private int threadNum;

        TaskThread(int threadNum) {
            this.threadNum = threadNum;
        }

        @Override
        public void run() {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " Index:" + this.threadNum);
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
        }
    }
}
